package service

import (
	"errors"
	uuid "github.com/satori/go.uuid"
	"go_shengshu_admin/model"
	"go_shengshu_admin/query"
	"go_shengshu_admin/repository"
)

type OrderService struct {
	Repo *repository.OrderRepository
}

type OrderSrv interface {
	List(req *query.ListQuery) (orders []*model.Order, err error)
	GetTotal() (total int64, err error)
	GetOrder(order model.Order) (*model.Order, error)
	Exist(id string) *model.Order
	Add(order model.Order) *model.Order
	Edit(order model.Order) (bool, error)
	Delete(id string) (bool, error)
}

func (srv *OrderService) List(req *query.ListQuery) (orders []*model.Order, err error) {
	return srv.Repo.List(req)
}

func (srv *OrderService) GetTotal() (total int64, err error) {
	return srv.Repo.GetTotal()
}

func (srv *OrderService) GetOrder(order model.Order) (*model.Order, error) {
	return srv.Repo.GetOrder(order)
}

func (srv *OrderService) Exist(id string) *model.Order {
	return srv.Repo.Exist(id)
}

func (srv *OrderService) Add(order model.Order) *model.Order {
	order.OrderId = uuid.NewV4().String()
	order.IsDeleted = false
	return srv.Repo.Add(order)
}

func (srv *OrderService) Edit(order model.Order) (bool, error) {
	if order.OrderId == "" {
		return false, errors.New("参数错误")
	}
	if srv.Repo.Exist(order.OrderId) != nil {
		return false, errors.New("订单不存在")
	}
	return srv.Repo.Edit(order)
}

func (srv *OrderService) Delete(id string) (bool, error) {
	if id == "" {
		return false, errors.New("参数错误")
	}
	if srv.Repo.Exist(id) != nil {
		return false, errors.New("订单不存在")
	}
	return srv.Repo.Delete(id)
}
